package org.jks.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.jks.report.model.ReportDAOrderModel;
import org.jks.report.model.ReportDeviceApplyModel;
import org.jks.util.DateUtil;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "DeviceApply")
public class DeviceApply implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(generator = "increment")
	@GenericGenerator(name = "increment", strategy = "increment")
	@Expose
	private long Id;
	@Column(name = "OrderNo", length = 64, nullable = false)
	@Expose
	private String OrderNo;
	@ManyToOne
	@JoinColumn(name = "Department", referencedColumnName = "Id", nullable = false)
	@Expose
	private Department Department;
	@ManyToOne
	@JoinColumn(name = "[Group]", referencedColumnName = "Id", nullable = false)
	@Expose
	private Flight Group;
	@Temporal(TemporalType.TIMESTAMP)
	@Expose
	private Date Time;
	@Lob
	@Expose
	private String Desp;
	@OneToMany(mappedBy = "DeviceApply", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true)
	@Expose
	private List<ApplyOrder> ApplyOrders;

	public long getId() {
		return Id;
	}

	public void setId(long id) {
		Id = id;
	}

	public String getOrderNo() {
		return OrderNo;
	}

	public void setOrderNo(String orderNo) {
		OrderNo = orderNo;
	}

	public Department getDepartment() {
		return Department;
	}

	public void setDepartment(Department department) {
		Department = department;
	}

	public Flight getGroup() {
		return Group;
	}

	public void setGroup(Flight group) {
		Group = group;
	}

	public Date getTime() {
		return Time;
	}

	public void setTime(Date time) {
		Time = time;
	}

	public String getDesp() {
		return Desp;
	}

	public void setDesp(String desp) {
		Desp = desp;
	}

	public List<ApplyOrder> getApplyOrders() {
		return ApplyOrders;
	}

	public void setApplyOrders(List<ApplyOrder> applyOrders) {
		ApplyOrders = applyOrders;
	}

	public ReportDeviceApplyModel toReportModel(){
		ReportDeviceApplyModel rdam = new ReportDeviceApplyModel();
		rdam.setId(this.Id + "");
		rdam.setOrderNo(this.OrderNo);
		rdam.setDepartment(this.Department.getName());
		rdam.setFlight(this.Group.getName());
		rdam.setDesp(this.Desp);
		rdam.setApplyTime(DateUtil.toString(this.Time, "yyyy-MM-dd HH:mm:ss"));
		if(this.ApplyOrders != null && this.ApplyOrders.size() > 0){
			List<ReportDAOrderModel> orders = new ArrayList<ReportDAOrderModel>();
			for(ApplyOrder ao : this.ApplyOrders){
				ReportDAOrderModel rdaom = new ReportDAOrderModel();
				rdaom.setDeviceName(ao.getDevice().getType().getName());
				rdaom.setDeviceModel(ao.getDevice().getModel());
				rdaom.setId(ao.getId() + "");
				rdaom.setCount(ao.getCount() + "");
				rdaom.setUnitPrice(ao.getUnitPrice() + "");
				orders.add(rdaom);
			}
			rdam.setOrders(orders);
		}
		return rdam;
		
	}
}
